package com.example.testproj.synchronizedTest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.testproj.R;

public class SynchronizedErrorActivity extends AppCompatActivity {
    private static final String TAG = "SynchronizedErrorActivi";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_synchronized_error);
        for (int i=0;i<3;i++){
            new MyThread().start();
        }
//        try {
//            new TestClass().wait(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    public static class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
           new SynchronizedErrorActivity.TestClass().test3();
        }
    }

    public static class TestClass{
        public synchronized void  synchronizedTestMeth(){
            Log.d(TAG, "synchronizedTestMeth: 方法开始");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "synchronizedTestMeth: 方法结束");
        }

        public void test2(){
            synchronized (this){
                Log.d(TAG, "synchronizedTestMeth: 方法开始");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "synchronizedTestMeth: 方法结束");
            }

        }

        public void test3(){
            synchronized (this.getClass()){
                Log.d(TAG, "synchronizedTestMeth: 方法开始");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "synchronizedTestMeth: 方法结束");
            }

        }
    }


}
